/**
 * FileName: ProductController
 * Author: Wb
 * Date: 2025/9/24 10:27
 * Description:
 * History:
 */
package org.test.device.controller;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.test.device.enty.ProductProperty;
import org.test.device.params.QueryProductParam;
import org.test.device.params.SaveProductParam;
import org.test.device.service.MqttService;
import org.test.device.service.ProductService;
import org.test.device.utils.ResultData;
import org.test.device.vo.product.ProductVoList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈商品Controller〉
 *
 * @author Wb
 * @create 2025/9/24
 * @since 1.0.0
 */
@Slf4j
@Tag(name = "商品管理接口", description = "商品注册、状态查询、数据上报相关接口")
@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    MqttService mqttService;


    /**
     * @Author Mr.Wang
     * @Description 查询商品列表信息
     * @Date 16:45 2025/9/27
     * @param
     **/
    @GetMapping("list")
    public ResultData getProductList(@ParameterObject QueryProductParam param) {
        PageHelper.startPage(param.getCurrentPage(), param.getPageSize());
        log.info("查询商品列表参数-入参:{}", JSON.toJSONString(param));
        List<ProductVoList> productVoLists = productService.getProductList(param);
        PageInfo<ProductVoList> pageInfo = new PageInfo<>(productVoLists);
        log.info("查询结果：{}，总数：{}，总页数：{}，当前页：{}", productVoLists, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum());
        return ResultData.pageResponse(productVoLists, pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum());
    }

    /**
    * @Author Mr.Wang
    * @Description 查询选择产品列表
    * @Date 15:04 2025/9/28
    * @param
    **/
    @GetMapping("choice")
    public ResultData getProductChoiceList() {
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> productVoLists = productService.getProductChoiceList();
        map.put("choiceList",productVoLists);
        return new ResultData("查询成功！",map);
    }

    /**
     * @Author Wb
     * @Description 新增商品
     * @Date 15:48 2025/9/25
     * @Param [param]
     * @Return org.test.device.utils.ResultData
     */
    @PostMapping
    public ResultData saveProduct(@RequestBody SaveProductParam param) {
        log.info("新增产品参数-入参:{}", JSON.toJSONString(param));
        productService.saveProduct(param);
        return new ResultData("添加成功！");
    }


    /**
    * @Author Mr.Wang
    * @Description 启用产品
    * @Date 09:51 2025/9/28
    * @param code 产品code
    **/
    @PutMapping("{code}/updateStatus")
    public ResultData updateProductStatus(@PathVariable String code,
                                          @RequestParam Integer status){
        log.info("开始启用产品code:{},status:{}",code,status);
        return productService.updateProductStatus(code,status);
    }

    /**
    * @Author Mr.Wang
    * @Description 修改产品信息
    * @Date 11:04 2025/9/28
    * @param code 产品id
    * @param param 修改数据
    **/
    @PutMapping("{code}")
    public ResultData updateProduct(@PathVariable String code,
                                    @RequestBody SaveProductParam param){
        log.info("修改产品信息code:{},修改信息：{}",code,JSON.toJSONString(param));
        return productService.updateProduct(code,param);
    }

    /**
    * @Author Mr.Wang
    * @Description 删除商品
    * @Date 13:54 2025/9/28
    * @param
    **/
    @DeleteMapping("{code}")
    public ResultData deletedProduct(@PathVariable String code){
        log.info("删除产品数据code:{}",code);

        return productService.deletedProduct(code);
    }

    /**
     * @Author Wb
     * @Description 发布指令到设备（MQTT）
     * @Date 14:10 2025/9/25
     * @Param [topic, payload]
     * @Return org.test.device.utils.ResultData
     */
    @PostMapping("publish")
    public ResultData publish(@RequestParam String topic, @RequestBody ProductProperty payload) {
        mqttService.publish(topic, payload.toString());
        return new ResultData("发送成功");
    }

}